package com.ninthgroup.service.Impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.ninthgroup.config.AlipayConfig;
import com.ninthgroup.service.AliPayService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：1452863884@qq.com rcg
 * @date ：Created in 2022/10/24 14:19
 * @description：支付功能的实现类
 * @modified By：
 * @version:1.0
 */
@Service
public class AliPayServiceImpl implements AliPayService {
    @Override
    public boolean orderPayConf(HttpServletResponse response, HttpServletRequest request) {
        System.out.println("=========支付宝异步回调========");
        Map<String, String> params = new HashMap<>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (String name : requestParams.keySet()) {
            params.put(name, request.getParameter(name));
            System.out.println(name + " = " + request.getParameter(name));
        }

        String outTradeNo = params.get("out_trade_no");
        String gmtPayment = params.get("gmt_payment");
        String alipayTradeNo = params.get("trade_no");

        String sign = params.get("sign");
        String content = AlipaySignature.getSignCheckContentV1(params);
        boolean checkSignature = false; // 验证签名
        try {
            checkSignature = AlipaySignature.rsa256CheckContent(content, sign, AlipayConfig.ALIPAY_PUBLIC_KEY, "UTF-8");
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        System.out.println(checkSignature);
        // 支付宝验签
        if (true) {
            // 验签通过
            System.out.println("交易名称: " + params.get("subject"));
            System.out.println("交易状态: " + params.get("trade_status"));
            System.out.println("支付宝交易凭证号: " + params.get("trade_no"));
            System.out.println("商户订单号: " + params.get("out_trade_no"));
            System.out.println("交易金额: " + params.get("total_amount"));
            System.out.println("买家在支付宝唯一id: " + params.get("buyer_id"));
            System.out.println("买家付款时间: " + params.get("gmt_payment"));
            System.out.println("买家付款金额: " + params.get("buyer_pay_amount"));


            return true;
        }


        return false;

    }
}